package com.hcc.gateway.app.filter;

import com.alibaba.fastjson.JSON;
import com.hcc.gateway.app.constant.HccGatewayAppConstant;
import com.hcc.gateway.app.utils.ParamConvertUtils;
import com.hcc.gateway.common.domain.RespBody;
import com.hcc.gateway.domain.enums.ApiParamMappingTypeEnum;
import com.hcc.gateway.domain.enums.ContentTypeEnum;
import com.hcc.gateway.domain.enums.ProtocolEnum;
import com.hcc.gateway.domain.po.ApiConfigPo;
import com.hcc.gateway.domain.vo.ApiParamMappingVo;
import com.hcc.gateway.netty.context.GatewayExchange;
import com.hcc.gateway.netty.filter.GatewayFilter;
import com.hcc.gateway.netty.filter.GatewayFilterChain;
import com.hcc.gateway.service.ApiParamMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * RespConvertFilter
 *
 * @author hushengjun
 * @date 2021/12/29
 */
@Slf4j
@Component
public class RespParamConvertFilter implements GatewayFilter {

    @Autowired
    private ApiParamMappingService apiParamMappingService;

    @Override
    public int getOrder() {
        return 300;
    }

    @Override
    public void filter(GatewayExchange exchange, GatewayFilterChain chain) {
        log.info("进入RespParamConvertFilter !");
        Object result = exchange.getAttributeByName(HccGatewayAppConstant.HCC_RESPONSE_OBJECT);
        if (result == null) {
            chain.filter(exchange);
            return;
        }

        // 执行响应转换逻辑
        ApiConfigPo apiConfig = exchange.getAttributeByName(HccGatewayAppConstant.HCC_API_CONFIG, ApiConfigPo.class);
        ApiParamMappingVo apiParamMapping = apiParamMappingService.findApiParamMappingByApiConfig(apiConfig.getId(), ApiParamMappingTypeEnum.RESP);
        String protocol = apiConfig.getProtocol();
        Object respResult = null;

        if (ProtocolEnum.HTTP.name().equalsIgnoreCase(protocol)) {
            RespBody respBody = (RespBody) result;
            if (ContentTypeEnum.JSON.getCode().equals(respBody.getContentType())) {
                Map<String, Object> respMap = JSON.parseObject(respBody.getStringBody());
                respResult = ParamConvertUtils.convertParam(respMap, apiParamMapping.getChildren());
            } else if (ContentTypeEnum.XML.getCode().equals(respBody.getContentType())) {
                // TODO xml转json
            } else if (ContentTypeEnum.TEXT_HTML.getCode().equals(respBody.getContentType())) {
                // html直接返回
                respResult = respBody.getBytes();
            } else {
                // 其它类型只要是字符串内容就返字符串回去，否则返字节回去
                respResult = respBody.getStringBody() != null ? respBody.getStringBody() : respBody.getBytes();
            }
        } else if (ProtocolEnum.DUBBO.name().equalsIgnoreCase(protocol)) {
            if (result instanceof Map) {
                respResult = ParamConvertUtils.convertParam((Map<String, Object>) result, apiParamMapping.getChildren());
            } else if (result instanceof List) {
                respResult = ParamConvertUtils.convertParamArr((List<Object>) result, apiParamMapping.getChildren());
            } else {
                respResult = result;
            }
        }
        exchange.addAttribute(HccGatewayAppConstant.HCC_RESP_RESULT, respResult);

        chain.filter(exchange);
    }

}
